package org.showminifier.web;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;

import javax.jdo.Query;
import javax.servlet.http.HttpServletRequest;

import org.showminifier.core.Options;

import com.google.appengine.api.datastore.DatastoreNeedIndexException;
import com.google.appengine.api.datastore.DatastoreTimeoutException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;

/**
*
* Show Minifier
* Author: Shihua Ma <beijing.josh@gmail.com>
* Copyright (c) 2010 Shihua Ma
* Licensed under the MIT license.
*
*/


public class WebContentUtil {
	
	public static final int ENTITIES_PER_PAGE = 20;
	
	private static final Logger log = Logger.getLogger(WebContentUtil.class.getName());

	
	public static List<WebContent> getContents(Long keyOffset, Date date, String lable, User user){
		
		PersistenceManager pm = PMF.get().getPersistenceManager(); 
	    Query query = pm.newQuery(WebContent.class);
	    query.declareImports("import java.util.Date;import com.google.appengine.api.users.User");
	    query.declareParameters("Long keyOffset, Date dateParam, String lableParam, User user");
	    query.setOrdering("id desc");
	    /*if(keyOffset != null)
	    	query.setRange(keyOffset, keyOffset + ENTITIES_PER_PAGE + 1);*/
	    StringBuilder filter = new StringBuilder();
	    if (keyOffset != null) {
	        if (filter.length() != 0) {
	          filter.append(" && ");
	        }
	        filter.append("id >= keyOffset");
	    }
	    if (date != null) {
	        if (filter.length() != 0) {
	          filter.append(" && ");
	        }
	        filter.append("requestDate >= dateParam");
	    }
	    if (lable != null) {
	        if (filter.length() != 0) {
	          filter.append(" && ");
	        }
	        filter.append("lable == lableParam");
	    }
	    if (user != null) {
	        if (filter.length() != 0) {
	          filter.append(" && ");
	        }
	        filter.append("requestUser == user");
	    }
	    if (filter.length() > 0) {
	        query.setFilter(filter.toString());
	     }
	    List<WebContent> content = null;
	    Object obj[] = new Object[]{keyOffset, date, lable , user};
	    try{
	    	content =(List<WebContent>) query.executeWithArray(obj);
	    } catch (DatastoreTimeoutException e) {
            log.severe(e.getMessage());
	    } catch(DatastoreNeedIndexException e) {
            log.severe(e.getMessage());
	    }
	    query.closeAll();
	    return content; 
	}
	
	public static WebContent insertContent(WebContent content){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		WebContent value = null;
		try {		
	        tx.begin();
            value = pm.makePersistent(content);
            tx.commit();
        } finally {
        	if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        return value;
	}
	
	
	public static PagePerformance insertPerformance(WebContent content,PagePerformance performance){
		insertContent(content);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		PagePerformance value = null;
		try {		
	        tx.begin();
            value = pm.makePersistent(performance);
            tx.commit();
        } finally {
        	if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        return value;
	}
	
}
